package com.sxfq.loansupermarket.backsystem.modular.theTest.service.impl;

import com.sxfq.loansupermarket.backsystem.common.persistence.model.User;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.theTest.dao.UserDao;
import com.sxfq.loansupermarket.backsystem.modular.theTest.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserDao dao;
    @Autowired
    private SqlMapper sqlMapper;

    @Transactional
    public int selectSameNameCountForUpdate(int id, String name) throws Exception {
        int count = dao.selectSameNameCount(id, name);
        return count;
    }
    @Transactional
    public int getRanking(int userId)  {
        int count = dao.getRanking(userId);
        return count;
    }
    @Transactional
    public void UpdateWithInfo(User info) throws Exception {
        dao.updateById(info);
    }

    @Transactional
    public Map<String, Object> getTestCount(Integer userId){
        String sql = "select " +
                " (select COUNT(*) from (select c.id from (" +
                " select a.id   from" +
                " the_test_info a left join user_for_test_info b on a.id=b.the_test_id " +
                " GROUP BY a.id ) c where" +
                " c.id  not in (select a.id  from the_test_info a left join user_for_test_info b on a.id=b.the_test_id " +
                " where b.user_id ="+userId+"  GROUP BY a.id ) ) theTest  )as theTestCount, " +
                " (select COUNT(*)from (" +
                " select a.id from the_test_info a left join user_for_test_info b on a.id=b.the_test_id " +
                " where b.user_id  ="+userId+"  and b.test_status=2  GROUP BY a.id " +
                " ) completeTest) as completeTestCount, " +
                " (select COUNT(*)from ( " +
                " select a.id from the_test_info a left join user_for_test_info b on a.id=b.the_test_id " +
                " where b.user_id  ="+userId+"  and b.test_status=1  GROUP BY a.id " +
                " ) incompleteTest) as incompleteTestCount " +
                " from dual";
        Map<String, Object> result = sqlMapper.selectOne(sql);
        return result.size() > 0 ? result : null;
    }
}
